package com.inei.eea2014.model.dao;

import java.util.ArrayList;
import java.util.List;
import com.inei.eea2014.model.connection.UtilDB;
import com.inei.eea2014.model.entity.Marco;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class MarcoDao implements ICRUD<Marco> {

	// variables
	private UtilDB util;
	private SQLiteDatabase db;
	private Cursor cursor = null;

	// constructor
	public MarcoDao(Context context) {
		util = new UtilDB(context);
	}

	// metodos CRUD
	@Override
	public Marco buscar(Marco o) {
		Marco obj = null;
		db = util.getReadableDatabase();

		cursor = db.rawQuery("select * from Marco where RUC=? AND USUARIO=?",
				new String[] { o.getRUC() + "", o.getUSUARIO() + "" });
		if (cursor.moveToFirst()) {
			obj = new Marco();
			obj.setRUC(cursor.getString(cursor.getColumnIndex("RUC")));
			obj.setRAZON_SOCIAL(cursor.getString(cursor
					.getColumnIndex("RAZON_SOCIAL")));
			obj.setORGANIZACION_JURIDICA(cursor.getString(cursor
					.getColumnIndex("ORGANIZACION_JURIDICA")));
			obj.setUBIGEO(cursor.getString(cursor.getColumnIndex("UBIGEO")));
			obj.setDEPT(cursor.getString(cursor.getColumnIndex("DEPT")));
			obj.setPROV(cursor.getString(cursor.getColumnIndex("PROV")));
			obj.setDIST(cursor.getString(cursor.getColumnIndex("DIST")));
			obj.setCIIU_V4(cursor.getString(cursor.getColumnIndex("CIIU_V4")));
			obj.setDESC_TIPO_VIA(cursor.getString(cursor
					.getColumnIndex("DESC_TIPO_VIA")));
			obj.setNOMBRE_VIA(cursor.getString(cursor
					.getColumnIndex("NOMBRE_VIA")));
			obj.setPUERTA(cursor.getString(cursor.getColumnIndex("PUERTA")));
			obj.setMANZANA(cursor.getString(cursor.getColumnIndex("MANZANA")));
			obj.setLOTE(cursor.getString(cursor.getColumnIndex("LOTE")));
			obj.setPISO(cursor.getString(cursor.getColumnIndex("PISO")));
			obj.setINTERIOR(cursor.getString(cursor.getColumnIndex("INTERIOR")));
			obj.setKILOMETRO(cursor.getString(cursor
					.getColumnIndex("KILOMETRO")));
			obj.setTELEFONO(cursor.getString(cursor.getColumnIndex("TELEFONO")));
			obj.setTELEFONO1(cursor.getString(cursor
					.getColumnIndex("TELEFONO1")));
			obj.setREFERENCIA(cursor.getString(cursor
					.getColumnIndex("REFERENCIA")));
			obj.setFAX(cursor.getInt(cursor.getColumnIndex("FAX")));
			obj.setEMAIL(cursor.getString(cursor.getColumnIndex("EMAIL")));
			obj.setWEB(cursor.getString(cursor.getColumnIndex("WEB")));
			obj.setUSUARIO(cursor.getString(cursor.getColumnIndex("USUARIO")));
			obj.setFINALIZADO(cursor.getString(cursor
					.getColumnIndex("FINALIZADO")));
			obj.setVALIDADO(cursor.getString(cursor.getColumnIndex("VALIDADO")));
		}

		Cerrar();
		return obj;
	}

	@Override
	public List<Marco> leer() {
		List<Marco> lista = null;
		db = util.getReadableDatabase();
		cursor = db.rawQuery("select * from Marco", null);
		if (cursor.moveToFirst()) {
			lista = new ArrayList<Marco>();
			while (!cursor.isAfterLast()) {
				Marco obj = new Marco();
				obj.setRUC(cursor.getString(cursor.getColumnIndex("RUC")));
				obj.setRAZON_SOCIAL(cursor.getString(cursor
						.getColumnIndex("RAZON_SOCIAL")));
				obj.setORGANIZACION_JURIDICA(cursor.getString(cursor
						.getColumnIndex("ORGANIZACION_JURIDICA")));
				obj.setUBIGEO(cursor.getString(cursor.getColumnIndex("UBIGEO")));
				obj.setDEPT(cursor.getString(cursor.getColumnIndex("DEPT")));
				obj.setPROV(cursor.getString(cursor.getColumnIndex("PROV")));
				obj.setDIST(cursor.getString(cursor.getColumnIndex("DIST")));
				obj.setCIIU_V4(cursor.getString(cursor
						.getColumnIndex("CIIU_V4")));
				obj.setDESC_TIPO_VIA(cursor.getString(cursor
						.getColumnIndex("DESC_TIPO_VIA")));
				obj.setNOMBRE_VIA(cursor.getString(cursor
						.getColumnIndex("NOMBRE_VIA")));
				obj.setPUERTA(cursor.getString(cursor.getColumnIndex("PUERTA")));
				obj.setMANZANA(cursor.getString(cursor
						.getColumnIndex("MANZANA")));
				obj.setLOTE(cursor.getString(cursor.getColumnIndex("LOTE")));
				obj.setPISO(cursor.getString(cursor.getColumnIndex("PISO")));
				obj.setINTERIOR(cursor.getString(cursor
						.getColumnIndex("INTERIOR")));
				obj.setKILOMETRO(cursor.getString(cursor
						.getColumnIndex("KILOMETRO")));
				obj.setTELEFONO(cursor.getString(cursor
						.getColumnIndex("TELEFONO")));
				obj.setTELEFONO1(cursor.getString(cursor
						.getColumnIndex("TELEFONO1")));
				obj.setREFERENCIA(cursor.getString(cursor
						.getColumnIndex("REFERENCIA")));
				obj.setFAX(cursor.getInt(cursor.getColumnIndex("FAX")));
				obj.setEMAIL(cursor.getString(cursor.getColumnIndex("EMAIL")));
				obj.setWEB(cursor.getString(cursor.getColumnIndex("WEB")));
				obj.setUSUARIO(cursor.getString(cursor
						.getColumnIndex("USUARIO")));
				obj.setFINALIZADO(cursor.getString(cursor
						.getColumnIndex("FINALIZADO")));
				obj.setVALIDADO(cursor.getString(cursor
						.getColumnIndex("VALIDADO")));
				lista.add(obj);
				cursor.moveToNext();
			}
		}
		Cerrar();
		return lista;
	}

	@Override
	public int crear(Marco o) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int actualizar(Marco o) {
		int resultado = 0;
		db = util.getWritableDatabase();
		db.beginTransaction();

		ContentValues valores = new ContentValues();
		valores.put("FINALIZADO", o.getFINALIZADO());
		valores.put("VALIDADO", o.getVALIDADO());

		resultado = (int) db.update("Marco", valores, "RUC=? AND USUARIO=?",
				new String[] { o.getRUC() + "", o.getUSUARIO() + "" });
		if (resultado > 0) {
			db.setTransactionSuccessful();
		}

		valores.clear();
		Cerrar();
		return resultado;
	}

	@Override
	public int eliminar(Marco o) {
		// TODO Auto-generated method stub
		return 0;
	}

	// otros metodos
	public List<Marco> leer(String USUARIO) {
		List<Marco> lista = null;
		db = util.getReadableDatabase();
		cursor = db.rawQuery("select * from Marco where USUARIO=?",
				new String[] { USUARIO + "" });
		if (cursor.moveToFirst()) {
			lista = new ArrayList<Marco>();
			while (!cursor.isAfterLast()) {
				Marco obj = new Marco();
				obj.setRUC(cursor.getString(cursor.getColumnIndex("RUC")));
				obj.setRAZON_SOCIAL(cursor.getString(cursor
						.getColumnIndex("RAZON_SOCIAL")));
				obj.setORGANIZACION_JURIDICA(cursor.getString(cursor
						.getColumnIndex("ORGANIZACION_JURIDICA")));
				obj.setUBIGEO(cursor.getString(cursor.getColumnIndex("UBIGEO")));
				obj.setDEPT(cursor.getString(cursor.getColumnIndex("DEPT")));
				obj.setPROV(cursor.getString(cursor.getColumnIndex("PROV")));
				obj.setDIST(cursor.getString(cursor.getColumnIndex("DIST")));
				obj.setCIIU_V4(cursor.getString(cursor
						.getColumnIndex("CIIU_V4")));
				obj.setDESC_TIPO_VIA(cursor.getString(cursor
						.getColumnIndex("DESC_TIPO_VIA")));
				obj.setNOMBRE_VIA(cursor.getString(cursor
						.getColumnIndex("NOMBRE_VIA")));
				obj.setPUERTA(cursor.getString(cursor.getColumnIndex("PUERTA")));
				obj.setMANZANA(cursor.getString(cursor
						.getColumnIndex("MANZANA")));
				obj.setLOTE(cursor.getString(cursor.getColumnIndex("LOTE")));
				obj.setPISO(cursor.getString(cursor.getColumnIndex("PISO")));
				obj.setINTERIOR(cursor.getString(cursor
						.getColumnIndex("INTERIOR")));
				obj.setKILOMETRO(cursor.getString(cursor
						.getColumnIndex("KILOMETRO")));
				obj.setTELEFONO(cursor.getString(cursor
						.getColumnIndex("TELEFONO")));
				obj.setTELEFONO1(cursor.getString(cursor
						.getColumnIndex("TELEFONO1")));
				obj.setREFERENCIA(cursor.getString(cursor
						.getColumnIndex("REFERENCIA")));
				obj.setFAX(cursor.getInt(cursor.getColumnIndex("FAX")));
				obj.setEMAIL(cursor.getString(cursor.getColumnIndex("EMAIL")));
				obj.setWEB(cursor.getString(cursor.getColumnIndex("WEB")));
				obj.setUSUARIO(cursor.getString(cursor
						.getColumnIndex("USUARIO")));
				obj.setFINALIZADO(cursor.getString(cursor
						.getColumnIndex("FINALIZADO")));
				obj.setVALIDADO(cursor.getString(cursor
						.getColumnIndex("VALIDADO")));
				lista.add(obj);
				cursor.moveToNext();
			}
		}
		Cerrar();
		return lista;
	}

	private void Cerrar() {
		if (cursor != null) {
			cursor.close();
		}
		if (db.inTransaction()) {
			db.endTransaction();
		}
		if (db.isOpen()) {
			db.close();
		}
	}
}
